﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;
using System.Security.Cryptography;
namespace _5ChickDeal.Admin.Control
{
    public partial class UserList : System.Web.UI.UserControl
    {
        protected void GridViewBind()
        {
            try
            {
                DataClassesDataContext db = new DataClassesDataContext();

                
                var source = from user in db.KhachHangs
                             select user;
                GridViewUser.DataSource = source;
                GridViewUser.DataBind();
            }

            catch { }
           
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                GridViewBind();
            }

        }

        private int GetMaxUserID()
        {
            DataClassesDataContext db = new DataClassesDataContext();
            int kq = 0;
            try
            {
                kq = db.KhachHangs.Max(e => e.id);
            }
            catch { }
            return kq;
        }

        private void Message(bool success, String message)
        {
            if (success)
            {
                LabelOk.Text = message;

            }
            else
            {
                label1.Text = message;
            }
            PanelOk.Visible = success;
            PanelError.Visible = !success;
        }

        protected void GridViewUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                try
                {
                    ImageButton imgbt = (ImageButton)e.Row.Cells[5].Controls[0];
                    imgbt.OnClientClick = "if(!confirm('Bạn thực sự muốn xóa?')) return false;";
                }
                catch
                {
                }
            }
        }

        protected void GridViewUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {

                DataClassesDataContext db = new DataClassesDataContext();
                var user = db.KhachHangs.Single(i => i.id == (int)this.GridViewUser.DataKeys[e.RowIndex].Value);
                db.KhachHangs.DeleteOnSubmit(user);
                db.SubmitChanges();
                PanelError.Visible = false;
                PanelOk.Visible = true;
                LabelOk.Text = "Đã xóa user " + user.email;
                GridViewBind();
            }

            catch
            {
                PanelError.Visible = true;
                PanelOk.Visible = false;
            }
        }

        protected void GridViewUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewUser.PageIndex = e.NewPageIndex;
            GridViewBind();
        }

        protected void GridViewUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewUser.EditIndex = -1;
            GridViewBind();
        }

        protected void GridViewUser_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewUser.EditIndex = e.NewEditIndex;
            GridViewBind();
            TextBox id = (TextBox)GridViewUser.Rows[e.NewEditIndex].Cells[0].Controls[0];
            id.Enabled = false;
            
        }

        protected void GridViewUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox hoTen = (TextBox)GridViewUser.Rows[e.RowIndex].Cells[2].Controls[0];
            TextBox email = (TextBox)GridViewUser.Rows[e.RowIndex].Cells[1].Controls[0];
            TextBox dienThoai = (TextBox)GridViewUser.Rows[e.RowIndex].Cells[3].Controls[0];
            try
            {
                DataClassesDataContext db = new DataClassesDataContext();
                KhachHang kh = db.KhachHangs.Single(k => k.id == (int)GridViewUser.DataKeys[e.RowIndex].Value);

                kh.email = email.Text;
                kh.dienthoai = dienThoai.Text;
                kh.hoten = hoTen.Text;

                db.SubmitChanges();
                Message(true, "Cập nhật thành công");
            }
            catch
            {
                Message(false, "Có lỗi xảy ra khi cập nhật");
            }
            GridViewUser.EditIndex = -1;
            GridViewBind();

        }

        protected void TimKiem_Click(object sender, EventArgs e)
        {

            try
            {
                DataClassesDataContext db = new DataClassesDataContext();

                var source = from user in db.KhachHangs
                             where user.hoten.Contains(TextBoxTimKiem.Text)
                             
                             ||user.email.Contains(TextBoxTimKiem.Text)
                             ||user.dienthoai.Contains(TextBoxTimKiem.Text)
                             select user;
                GridViewUser.DataSource = source;
                GridViewUser.DataBind();
            }
            catch { }
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            System.Text.RegularExpressions.Regex regEmail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (!regEmail.IsMatch(TextBoxEmail.Text))
            {
                Message(false, "Email bạn nhập không hợp lệ");
                return;
            }
            DataClassesDataContext db = new DataClassesDataContext();
            KhachHang check=null;
            try
            {
                check = db.KhachHangs.ToList().Find(c => c.email == TextBoxEmail.Text);
            }
            catch
            {
            }
            if (check == null)
            {
                check = new KhachHang();
                check.id = GetMaxUserID() + 1;
                check.email = TextBoxEmail.Text;
                check.hoten = TextBoxHoten.Text;
                check.matkhau = EncoderPassword.GetMd5Hash(TextBoxMatKhau.Text);
                check.dienthoai = TextBoxDienThoai.Text;
                db.KhachHangs.InsertOnSubmit(check);
                db.SubmitChanges();
                Message(true, "Thêm thành công");
                GridViewBind();
            }
            else
            {
                Message(false, "Email đã được đăng ký trước đó rồi");
            }

        }


    }
}